<?php

/**
 * Settings - Password
 * 
 * @project : Loobica
 * @author  : Dino
 * @date    : 27 Mar 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Password extends LB_Auth_Controller {

  /**
   * Current User Pasword Change
   * 
   */
  public function index() {

    // Init vars
    $status = false;
    $user_password_current = null;
    $user_password_new = null;
    $user_password_new_confirm = null;
    $form_error = array('type' => null, 'message' => null);

    // Getting user data
    $user_data = $this->user_model->get_user_id($this->signed_user['user_id'], 1, true);

    // Form submit
    if ($this->is_post()) {

      // Getting input values
      $user_password_current = (string) $this->input->post('user_password_current', true);
      $user_password_new = (string) $this->input->post('user_password_new', true);
      $user_password_new_confirm = (string) $this->input->post('user_password_new_confirm', true);

      // Validating current password
      if (!empty($user_password_current) && strlen($user_password_current) > 4) {

        // Validating new password
        if (!empty($user_password_new) && strlen($user_password_new) > 4) {

          // Validating confirm password
          if (!empty($user_password_new_confirm) && strlen($user_password_new_confirm) > 4) {

            // Validating new password and confirm password
            if ($user_password_new == $user_password_new_confirm) {

              // Checking current password
              if ($this->loobica_hash($user_password_current) == $user_data['user_password']) {

                // Update user password
                $user_set = array();
                $user_set['user_password'] = $this->loobica_hash($user_password_new);
                $this->user_model->set_user($user_set, $user_data['user_id']);

                // Reset fields
                $status = true;
                $user_password_current = null;
                $user_password_new = null;
                $user_password_new_confirm = null;
              } else {

                // Invalid password
                $form_error = array('type' => 1, 'message' => 'Current password is wrong');
              }
            } else {

              // Invalid password
              $form_error = array('type' => 2, 'message' => 'New password and confirm new password must be same');
            }
          } else {

            // Invalid password
            $form_error = array('type' => 3, 'message' => 'Confirm password is invalid');
          }
        } else {

          // Invalid password
          $form_error = array('type' => 2, 'message' => 'New password is invalid');
        }
      } else {

        // Invalid password
        $form_error = array('type' => 1, 'message' => 'Current password is invalid');
      }
    }

    // Preparing view data
    $view_data = array();
    $view_data['title'] = 'Settings - Password';
    $view_data['user_data'] = $user_data;
    $view_data['status'] = $status;
    $view_data['form_error'] = $form_error;
    $view_data['user_password_current'] = $user_password_current;
    $view_data['user_password_new'] = $user_password_new;
    $view_data['user_password_new_confirm'] = $user_password_new_confirm;
    $this->load_view('settings/password', $view_data);
  }

}

?>